﻿using System.IO;
using System.Windows;
using System.Windows.Controls;
using forms = System.Windows.Forms;

namespace HandTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        string ygoFolder = System.IO.Path.GetFullPath("..\\YGOPro\\");
        DirectoryInfo deckDirectory;

        readonly string appFolder = Directory.GetCurrentDirectory();
        readonly DirectoryInfo testDirectory;

        int currentTreeID = 0;
        FileInfo currentTestFile;
        TreeViewItem currentPoint;
        TreeViewItem currentCombo;
        TreeViewItem currentHand;
        TreeViewItem selectdTreeViewItem;

        public MainWindow()
        {
            InitializeComponent();

            if (!Directory.Exists(appFolder + "\\test"))
                Directory.CreateDirectory(appFolder + "\\test");
            testDirectory = new DirectoryInfo(appFolder + "\\test");
            testFileList.ItemsSource = testDirectory.GetFiles("*.test");
            LoadDeckFileList();
        }

        private void SetYGOProFolder_Click(object sender, RoutedEventArgs e)
        {
            var fbd = new forms.FolderBrowserDialog { SelectedPath = ygoFolder };
            if (fbd.ShowDialog() == forms.DialogResult.OK)
            {
                string strPath = fbd.SelectedPath;
                ygoFolder = strPath.EndsWith("\\") ? strPath : strPath + "\\";
                LoadDeckFileList();
            }
        }

        private void DeckFileList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var deckFile = deckFileList.SelectedItem as FileInfo;
            LoadDeckFile(deckFile);
        }

        private void MainDeckListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = (sender as ListBox).SelectedItem as string;
            var card = new TreeViewItem() { Header = item };
            if (currentTreeID == 1)
                currentHand.Items.Add(card);
            else if (currentTreeID == 2 || currentTreeID == 3)
                currentPoint.Items.Add(card);
            selectdTreeViewItem = null;
        }

        private void NewCombo_Click(object sender, RoutedEventArgs e)
        {
            currentTreeID = 1;
            AddPoint(baseComboTree, basePoint.Text);
            AddCombo();
            AddHand();
        }

        private void NewCard_Click(object sender, RoutedEventArgs e)
        {
            currentTreeID = 1;
            if (currentCombo == null)
            {
                AddPoint(baseComboTree, basePoint.Text);
                AddCombo();
            }
            AddHand();
        }

        private void AddReExCard_Click(object sender, RoutedEventArgs e)
        {
            currentTreeID = 2;
            AddPoint(repeatableExtraTree, repeatableExtraPoint.Text);
        }

        private void AddUnExCard_Click(object sender, RoutedEventArgs e)
        {
            currentTreeID = 3;
            AddPoint(unrepeatableExtraTree, unrepeatableExtraPoint.Text);
        }

        private void Tree_SelectionChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView s = sender as TreeView;
            if (s == baseComboTree)
                currentTreeID = 1;
            else if (s == repeatableExtraTree)
                currentTreeID = 2;
            else if (s == unrepeatableExtraTree)
                currentTreeID = 3;

            if ((selectdTreeViewItem = e.NewValue as TreeViewItem) != null)
                SetCurrent(selectdTreeViewItem);
        }

        private void DeleteItem_Click(object sender, RoutedEventArgs e)
        {
            if (selectdTreeViewItem != null)
            {
                if (selectdTreeViewItem.Parent is TreeViewItem parent)
                {
                    parent.Items.Remove(selectdTreeViewItem);
                    SetCurrent(parent);
                }
                else
                {
                    var tree = selectdTreeViewItem.Parent as TreeView;
                    tree.Items.Remove(selectdTreeViewItem);
                }
            }
        }

        private void StartTest_Click(object sender, RoutedEventArgs e)
        {
            if (cardsInMainDeck != null &&
                int.TryParse(testTimes.Text, out int cycles) &&
                int.TryParse(cardsInHand.Text, out int cards) &&
                cardsInMainDeck.Count > cards)
                HandTest(cycles, cards);
        }

        private void TestFileList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            currentTestFile = testFileList.SelectedItem as FileInfo;
            if (currentTestFile != null)
                LoadAsXml(currentTestFile);
        }

        private void SaveTestFile_Click(object sender, RoutedEventArgs e)
        {
            if (testFileList.SelectedItem is FileInfo testfile)
                SaveAsXml(testfile.FullName);
        }

        private void SaveTestFileAs_Click(object sender, RoutedEventArgs e)
        {
            if (testFileName.Text != "")
                SaveAsXml(appFolder + "\\test\\" + testFileName.Text + ".test");
        }

        private void DeleteTestFile_Click(object sender, RoutedEventArgs e)
        {
            if (currentTestFile != null)
            {
                currentTestFile.Delete();
                ClearComboTrees();
                testFileList.ItemsSource = testDirectory.GetFiles("*.test");
            }
        }

        private void ClearComboTrees_Click(object sender, RoutedEventArgs e)
        {
            ClearComboTrees();
        }

    }
}
